package com.sz.biz.common.customer.service.impl;

import com.sz.biz.common.constants.CusUserConfigKeys;
import com.sz.biz.common.customer.entity.CusUserConfig;
import com.sz.biz.common.customer.service.CustomerUserConfigService;
import com.sz.biz.common.customer.dto.CusUserConfigDto;
import com.sz.common.base.service.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Function: UserConfigServiceImpl <br>
 * Author: qiankun <br>
 * Date: 2017-05-06 16:59:00
 */
@Service("CustomerUserConfigService")
public class CustomerUserConfigServiceImpl extends AbstractService implements CustomerUserConfigService {

    @Override
    public CusUserConfigDto findAllConfigByUserId(int userId) {
        List<CusUserConfig> cucs = dao.findForList("CusUserConfigMapper.findAllByUserId", userId, CusUserConfig.class);
        if (cucs.size() == 0) {
            return null;
        }
        CusUserConfigDto dto = new CusUserConfigDto();
        for (CusUserConfig cuc : cucs) {
            if (cuc.getParamName().equals(CusUserConfigKeys.KEY_PAGE_SIZE)) {
                dto.setPageSize(cuc.getParamValue());
            }
            if (cuc.getParamName().equals(CusUserConfigKeys.KEY_I18N)) {
                dto.setI18n(cuc.getParamValue());
            }
        }
        if (!StringUtils.hasLength(dto.getI18n())) {
            dto.setI18n("zh-CN");
        }
        if (!StringUtils.hasLength(dto.getPageSize())) {
            dto.setPageSize("50");
        }
        return dto;
    }

    @Override
    public String getUserPreferredLanguage(int userId) {
        CusUserConfigDto dto = findAllConfigByUserId(userId);
        if (dto == null) {
            return "default";
        }
        return dto.getI18n();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveUserConfig(CusUserConfigDto cusUserConfigDto) {
        if (StringUtils.hasLength(cusUserConfigDto.getI18n())) {
            CusUserConfig cuc = new CusUserConfig();
            cuc.setUserId(cusUserConfigDto.getUserId());
            cuc.setParamName(CusUserConfigKeys.KEY_I18N);
            cuc.setParamValue(cusUserConfigDto.getI18n());
            saveOrUpdate(cuc);
        }

        if (StringUtils.hasLength(cusUserConfigDto.getPageSize())) {
            CusUserConfig cuc = new CusUserConfig();
            cuc.setUserId(cusUserConfigDto.getUserId());
            cuc.setParamName(CusUserConfigKeys.KEY_PAGE_SIZE);
            cuc.setParamValue(cusUserConfigDto.getPageSize());
            saveOrUpdate(cuc);
        }
    }

    private void saveOrUpdate(CusUserConfig cusUserConfig) {
        CusUserConfig cuc = findByUserIdAndParamName(cusUserConfig);
        if (cuc == null) {
            dao.save("CusUserConfigMapper.insert", cusUserConfig);
        } else {
            cuc.setParamValue(cusUserConfig.getParamValue());
            dao.update("CusUserConfigMapper.update", cuc);
        }
    }

    @Override
    public CusUserConfig findByUserIdAndParamName(CusUserConfig cusUserConfig) {
        return (CusUserConfig) dao.findForObject("CusUserConfigMapper.findByUserIdAndParamName", cusUserConfig);
    }

    @Override
    protected String getMapperNamespace() {
        return "CusUserConfigMapper";
    }

}
